package com.ztgf.shopping.service.epg;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.shopping.common.config.YmlConfig;
import com.ztgf.shopping.mapper.epg.EpgAggregationBannerMapper;
import com.ztgf.shopping.models.bo.epg.EpgAggregationBannerBO;
import com.ztgf.shopping.models.bo.epg.EpgAggregationBannerSaveBO;
import com.ztgf.shopping.models.constant.ShoppingConstant;
import com.ztgf.shopping.models.dto.epg.EpgAggregationBannerDTO;
import com.ztgf.shopping.models.enums.ShoppingCodeEnum;
import com.ztgf.shopping.models.service.epg.IEpgAggregationBannerService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 聚合页banner控制器
 *
 * @author YUE
 * @Date 2020-06-04 15:36:46
 */
@DubboService
public class EpgAggregationBannerServiceImpl extends ServiceImpl<EpgAggregationBannerMapper, EpgAggregationBannerDTO> implements IEpgAggregationBannerService{

    @Resource
    private YmlConfig ymlConfig;

    @Autowired
    private EpgAggregationBannerMapper epgAggregationBannerMapper;

    @Override
    public boolean add(EpgAggregationBannerSaveBO saveBO) {
        EpgAggregationBannerDTO dto = new EpgAggregationBannerDTO();
        BeanUtils.copyProperties(saveBO,dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }

    /**
     * 删除功能功能相关的活动banner
     * @return
     */
    public boolean deleteByModuleId(Long moduleId){
        Map<String, Object> columnMap = new HashMap<>();
        columnMap.put("module_id",moduleId);
        return epgAggregationBannerMapper.deleteByMap(columnMap) > 0;
    }

    /**
     * 删除功能功能相关的活动banner
     * @return
     */
    public boolean deleteByTemplateId(Long templateId){
        Map<String, Object> columnMap = new HashMap<>();
        columnMap.put("templet_id",templateId);
        return epgAggregationBannerMapper.deleteByMap(columnMap) > 0;
    }


    /**
     * 根据相关信息查找 banner 有页码
     * @param epgAggregationBannerBO
     * @return
     */
    @Override
    public Page<EpgAggregationBannerDTO> getBySelectBannerInfoPage(EpgAggregationBannerBO epgAggregationBannerBO) {
        epgAggregationBannerBO = addMustData(epgAggregationBannerBO);
        Wrapper<EpgAggregationBannerDTO> wrapper = getBySelectBannerInfo(epgAggregationBannerBO);
        return selectPage(new Page<>(epgAggregationBannerBO.getPageNo(),epgAggregationBannerBO.getPageSize()), wrapper);
    }

    /**
     * 添加必要数据
     * @param epgAggregationBannerBO
     * @return
     */
    private EpgAggregationBannerBO addMustData(EpgAggregationBannerBO epgAggregationBannerBO){
        epgAggregationBannerBO.setIsPublish(ShoppingCodeEnum.PUBLIC_1.getCode());
        epgAggregationBannerBO.setStatus(ShoppingCodeEnum.STATUS_0.getCode());
        epgAggregationBannerBO.setSort(ShoppingCodeEnum.SORT_ASC.getCode());
        return epgAggregationBannerBO;
    }

    /**
     * 根据相关信息查找 banner 无页码
     * @param epgAggregationBannerBO
     * @return
     */
    @Override
    public List<EpgAggregationBannerDTO> getBySelectBannerInfoNoPage(EpgAggregationBannerBO epgAggregationBannerBO) {
        epgAggregationBannerBO = addMustData(epgAggregationBannerBO);
        Wrapper<EpgAggregationBannerDTO> wrapper = getBySelectBannerInfo(epgAggregationBannerBO);
        if(epgAggregationBannerBO.getPageNo() == 0 && epgAggregationBannerBO.getPageSize() == 0)
            return epgAggregationBannerMapper.selectPage( new Page<>(0, ShoppingConstant.PLATFORM_BANNER_NUM),wrapper);
        return selectList(wrapper);
    }

    private Wrapper<EpgAggregationBannerDTO> getBySelectBannerInfo(EpgAggregationBannerBO epgAggregationBannerBO) {
        Wrapper<EpgAggregationBannerDTO> wrapper = new EntityWrapper<>();
        EpgAggregationBannerDTO epgAggregationBannerDTO = new EpgAggregationBannerDTO();
        BeanUtils.copyProperties(epgAggregationBannerBO,epgAggregationBannerDTO);
        wrapper.eq("status", epgAggregationBannerBO.getStatus());
        wrapper.eq("is_publish", epgAggregationBannerBO.getIsPublish());
        wrapper.eq("module_id", epgAggregationBannerBO.getModuleId());
        if(ShoppingCodeEnum.SORT_DESC.getCode() == epgAggregationBannerBO.getSort().intValue())//降序
            wrapper.orderBy("sort", false);
        else
            wrapper.orderBy("sort", true);
        return wrapper;
    }
}
